package com.peoit.honeycomb.dto.security;

import com.google.common.base.Function;
import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.peoit.honeycomb.domain.security.Person;
import com.peoit.honeycomb.domain.security.Role;
import com.peoit.honeycomb.dto.BaseDto;

import java.util.*;

/**
 * 后台用户dto
 * Created by zhouqiu on 2015/10/15.
 */
public class PersonDto extends BaseDto {

    private  String name;

    private String password;
    //描述
    private String description;

    private Set<RoleDto> roles = new HashSet<RoleDto>();

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public Set<RoleDto> getRoles() {
        return roles;
    }

    public void setRoles(Set<RoleDto> roles) {
        this.roles = roles;
    }

    public PersonDto toDto(Person person){
        PersonDto dto = new PersonDto();
        dto.setId(person.getId());
        dto.setName(person.getName());
        dto.setDescription(person.getDescription());
        dto.setPassword(person.getPassword());
        Set<RoleDto> sets = Sets.newHashSet();
        for(Role role : person.getRoles()){
            RoleDto roleDto = new RoleDto();
            RoleDto roleDto1 = roleDto.toDto(role);
            sets.add(roleDto1);
        }
        dto.setRoles(sets);
        return dto;
    }

    public Person toEntity(PersonDto personDto,Set<Role> roles) {
        Person person = new Person();
        person.setId(personDto.getId());
        person.setName(personDto.getName());
        person.setDescription(personDto.getDescription());
        person.setPassword(personDto.getPassword());
        person.setRoles(roles);
        return person;
    }

    public List<PersonDto> toPageDto(List<Person> personList){
        Collection collection = Collections2.transform(personList, new Function<Person, PersonDto>() {
            @Override
            public PersonDto apply(Person input) {
                return toDto(input);
            }
        });

        ArrayList<PersonDto> personDtos = Lists.newArrayList(collection);
        return  personDtos;
    }
}
